package ca.liu.scaffoldingmvctest.web.rest;

import ca.liu.scaffoldingmvctest.dao.StLogDAO;
import ca.liu.scaffoldingmvctest.dao.StUserDAO;

import ca.liu.scaffoldingmvctest.domain.StLog;
import ca.liu.scaffoldingmvctest.domain.StUser;

import ca.liu.scaffoldingmvctest.service.StLogService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Spring Rest controller that handles CRUD requests for StLog entities
 * 
 */

@Controller("StLogRestController")
public class StLogRestController {

	/**
	 * DAO injected by Spring that manages StLog entities
	 * 
	 */
	@Autowired
	private StLogDAO stLogDAO;

	/**
	 * DAO injected by Spring that manages StUser entities
	 * 
	 */
	@Autowired
	private StUserDAO stUserDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for StLog entities
	 * 
	 */
	@Autowired
	private StLogService stLogService;

	/**
	 * Select an existing StLog entity
	 * 
	 */
	@RequestMapping(value = "/StLog/{stlog_id}", method = RequestMethod.GET)
	public StLog loadStLog(@PathVariable Integer stlog_id) {
		return stLogDAO.findStLogByPrimaryKey(stlog_id);
	}

	/**
	 * Get StUser entity by StLog
	 * 
	 */
	@RequestMapping(value = "/StLog/{stlog_id}/stUser", method = RequestMethod.GET)
	public StUser getStLogStUser(@PathVariable Integer stlog_id) {
		return stLogDAO.findStLogByPrimaryKey(stlog_id).getStUser();
	}

	/**
	 * Show all StLog entities
	 * 
	 */
	@RequestMapping(value = "/StLog", method = RequestMethod.GET)
	public List<StLog> listStLogs() {
		return new java.util.ArrayList<StLog>(stLogService.loadStLogs());
	}

	/**
	 * Create a new StUser entity
	 * 
	 */
	@RequestMapping(value = "/StLog/{stlog_id}/stUser", method = RequestMethod.POST)
	public StUser newStLogStUser(@PathVariable Integer stlog_id, @ModelAttribute StUser stuser) {
		stLogService.saveStLogStUser(stlog_id, stuser);
		return stUserDAO.findStUserByPrimaryKey(stuser.getId());
	}

	/**
	 * Save an existing StUser entity
	 * 
	 */
	@RequestMapping(value = "/StLog/{stlog_id}/stUser", method = RequestMethod.PUT)
	public StUser saveStLogStUser(@PathVariable Integer stlog_id, @ModelAttribute StUser stuser) {
		stLogService.saveStLogStUser(stlog_id, stuser);
		return stUserDAO.findStUserByPrimaryKey(stuser.getId());
	}

	/**
	 * Delete an existing StUser entity
	 * 
	 */
	@RequestMapping(value = "/StLog/{stlog_id}/stUser/{stuser_id}", method = RequestMethod.DELETE)
	public void deleteStLogStUser(@PathVariable Integer stlog_id, @PathVariable Integer related_stuser_id) {
		stLogService.deleteStLogStUser(stlog_id, related_stuser_id);
	}

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	 * Delete an existing StLog entity
	 * 
	 */
	@RequestMapping(value = "/StLog/{stlog_id}", method = RequestMethod.DELETE)
	public void deleteStLog(@PathVariable Integer stlog_id) {
		StLog stlog = stLogDAO.findStLogByPrimaryKey(stlog_id);
		stLogService.deleteStLog(stlog);
	}

	/**
	 * Create a new StLog entity
	 * 
	 */
	@RequestMapping(value = "/StLog", method = RequestMethod.POST)
	public StLog newStLog(@ModelAttribute StLog stlog) {
		stLogService.saveStLog(stlog);
		return stLogDAO.findStLogByPrimaryKey(stlog.getId());
	}

	/**
	 * View an existing StUser entity
	 * 
	 */
	@RequestMapping(value = "/StLog/{stlog_id}/stUser/{stuser_id}", method = RequestMethod.GET)
	public StUser loadStLogStUser(@PathVariable Integer stlog_id, @PathVariable Integer related_stuser_id) {
		StUser stuser = stUserDAO.findStUserByPrimaryKey(related_stuser_id, -1, -1);

		return stuser;
	}

	/**
	 * Save an existing StLog entity
	 * 
	 */
	@RequestMapping(value = "/StLog", method = RequestMethod.PUT)
	public StLog saveStLog(@ModelAttribute StLog stlog) {
		stLogService.saveStLog(stlog);
		return stLogDAO.findStLogByPrimaryKey(stlog.getId());
	}
}